package com.net.oil.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.net.oil.R;
import com.net.oil.main.adapter.IntroduceViewPagerAdapter;
import com.net.oil.util.DisplayUtil;
import com.net.oil.util.SharedPreferencesKey;
import com.net.oil.util.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

public class IntroduceActivity extends AppCompatActivity {

    @BindView(R.id.introduce_view_pager)
    ViewPager introduceViewPager;
    @BindView(R.id.introduce_dot_group)
    RadioGroup introduceDotGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introduce);
        ButterKnife.bind(this);

        initView();
        SharedPreferencesUtil.setBoolenParm(this, SharedPreferencesKey.IS_FIRST_INSTALL, false);
    }

    private void initView() {
        final List<ImageView> imageViews = new ArrayList<>();

        RadioGroup.LayoutParams buttonParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        buttonParams.setMargins(DisplayUtil.dip2px(5), 0,
                DisplayUtil.dip2px(5), 0);
        int[] images = {R.drawable.introduce_image1, R.drawable.introduce_image2, R.drawable.introduce_image3, R.drawable.introduce_image5};

        for (int i = 0; i < 4; i++){
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(images[i]);
            imageViews.add(imageView);

            RadioButton button = new RadioButton(this);
            button.setButtonDrawable(null);
            button.setLayoutParams(buttonParams);
            button.setWidth(DisplayUtil.dip2px(4));
            button.setHeight(DisplayUtil.dip2px(4));
            button.setId(i);
            button.setBackgroundResource(R.drawable.banner_dot_bg_selector);
            if (i == 0) {
                button.setChecked(true);
            }
            introduceDotGroup.addView(button);
        }

        IntroduceViewPagerAdapter adapter = new IntroduceViewPagerAdapter(imageViews);
        introduceViewPager.setAdapter(adapter);
        introduceViewPager.setCurrentItem(0);
        introduceViewPager.setOffscreenPageLimit(imageViews.size());

        introduceViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                introduceDotGroup.check(position);
                if (position == imageViews.size() - 1){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(new Intent(IntroduceActivity.this, MainActivity.class));
                            finish();
                        }
                    }, 1000);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
